import React, {ReactNode} from 'react';
import {DeviceEventEmitter, StyleSheet, View} from "react-native";
import {NavigatorAppStatesType} from "./components/navigator/NavigatorTypes";
import NavigatorConfig from "./components/navigator/NavigatorConfig";
import NavigatorBar from "./components/navigator/NavigatorFactory";
import ToastRootView from "./components/toast/ToastRootView";
import {appInitializer} from "./AppInitializer";
import AddFriendModal from "./page/modal/AddFriendModal";
import AddRoomModal from "./page/modal/AddRoomModal";
import SystemMessagePage from "./page/SystemMessagePage";
import NavigatorViewConfig from "./components/navigator/NavigatorViewConfig";
import {ScreenDimensions} from "./libs/utils/ScreenDimensions";
import ChatWinPage, {ChatRightNavigationBtn} from "./page/ChatWinPage";
import ModifyUserInfoPage from "./page/ModifyUserInfoPage";
import {BroadcastEvent} from "./events/BroadcastEvent";
import {EChatIMSDK} from "./libs/sdk/echatim/core/sdk/EChatIMSDK";
import Toast from "./components/toast/Toast";
import LoginPage from "./page/LoginPage";
import ModifyFriendInfoPage from "./page/ModifyFriendInfoPage";
import ModifyRoomInfoPage from "./page/ModifyRoomInfoPage";
import TabNavigatorComponent from "./components/TabNavigatorComponent";


const styles = StyleSheet.create({
    box: {
        flex: 1,
        backgroundColor:ScreenDimensions.themeBackgroundColor,
        width:ScreenDimensions.screen.width,
        height:ScreenDimensions.screen.height,
        position: 'relative'
    },
    text: {fontWeight: 'bold'}
});





export default class WebClientApp extends React.Component<any, NavigatorAppStatesType> {
    private im:EChatIMSDK | any;

    constructor(props: any) {
        super(props);

        // appInitializer.init();

        // 选中 session
        this.state = {
            // selectedTab: 'session',
            navigator: {root: true},
        };
        DeviceEventEmitter.addListener(BroadcastEvent.EVENT_SDK_INIT, this.onSDKInit.bind(this));
    }

    onSDKInit(im:EChatIMSDK){
        this.im = im;
        this.forceUpdate();
    }
    onDoSDKLogin(auid:string, token:string){
        if(!auid || !token){
            Toast.show('SDK用户账号或密码为空.');
            return;
        }
        appInitializer.init(auid, token);
    }
    renderTabNavigatorContent(): ReactNode {
        return (
            <TabNavigatorComponent selectedTab={'session'}/>
        )
    }


    render(): ReactNode {
        if(this.im === undefined){
            return (
                <View style={styles.box} nativeID={'client-app'}>
                    <LoginPage onLogin={(auid, token)=>{this.onDoSDKLogin(auid, token)}}/>
                    <ToastRootView/>
                </View>
            );
        }
        else {
            const tabView = this.state.navigator.root
                ? this.renderTabNavigatorContent()
                : null;
            const navigationView = !this.state.navigator.root
                ? this.renderNavigatorContent()
                : null;
            return (
                <View style={styles.box} nativeID={'client-app'}>
                    {tabView}
                    {navigationView}
                    <ToastRootView/>
                </View>
            );
        }
    }

    renderNavigatorContent(): ReactNode {
        return (
            <View nativeID={this.NAVIGATION_CONTAIN_ID}>
            </View>
        );
    }


    // ===============================================================================================================
    NAVIGATION_CONTAIN_ID = 'navigation-content';
    private navigatorViewsRouter = {
        AddFriendModal:new NavigatorViewConfig(()=><AddFriendModal />, '添加好友', null, null),
        ModifyUserInfoPage:new NavigatorViewConfig((paramsKey)=><ModifyUserInfoPage paramsKey={paramsKey} />, '编辑用户信息', null, null),
        ModifyFriendInfoPage:new NavigatorViewConfig((paramsKey)=><ModifyFriendInfoPage paramsKey={paramsKey} />, '编辑好友信息', null, null),
        ModifyRoomInfoPage:new NavigatorViewConfig((paramsKey)=><ModifyRoomInfoPage paramsKey={paramsKey} />, '修改讨论组', null, null),
        AddRoomModal:new NavigatorViewConfig((paramsKey)=><AddRoomModal paramsKey={paramsKey} />, '添加讨论组', null, null),
        SystemMessagePage:new NavigatorViewConfig(()=><SystemMessagePage />, '系统消息', null, null),

        ChatWinPage:new NavigatorViewConfig((paramsKey)=><ChatWinPage  paramsKey={paramsKey} />, '聊天', null, <ChatRightNavigationBtn />),

    };
    UNSAFE_componentWillMount(): void {
        NavigatorBar.initConfig(this, this.NAVIGATION_CONTAIN_ID, new NavigatorConfig(), this.navigatorViewsRouter);
    }

    componentDidMount(): void {
    }
}

